<?php

/**
 * This is the model class for table "iteam_admin".
 *
 * The followings are the available columns in table 'iteam_admin':
 * @property string $aid
 * @property string $name
 * @property string $password
 * @property integer $power
 * @property integer $status
 * @property string $dateline
 */
class Admin extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Admin the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'iteam_admin';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, password', 'required'),
            array('power, status', 'numerical', 'integerOnly' => true),
            array('name, password', 'length', 'max' => 50),
            array('dateline', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('aid, name, password, power, status, dateline', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'aid' => 'Aid',
            'name' => 'Name',
            'password' => 'Password',
            'power' => 'Power',
            'status' => 'Status',
            'dateline' => 'Dateline',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('aid', $this->aid, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('power', $this->power);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function validatePassword($password) {
        return md5($password) === $this->password;
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = date('Y-m-d H:i:s');
            }
        }
        return true;
    }

    //根据用户的权限（power查找用户）
    public function getUserByPower($power) {
        return $this->findAll('power=:Power', array('Power' => (int) $power));
    }

    public function getUsers($power) {
        $users = Admin::model()->getUserByPower($power);
        if ($users) {
            $userArr = array();
            foreach ($users as $user) {
                $userArr[] = $user['name'];
            }
        }
        return $userArr;
    }

}